const jwt = require('jsonwebtoken'),
    { JWT_SECURITY_KEY } = require('./security')

const getUserInfo = async (ctx, next) => {
    const { authorization } = ctx.req.headers;
    //请求头中携带token
    if (typeof authorization !== 'undefined') {
        const token = authorization.split(' ')[1];
        try {
            const user = jwt.verify(token, JWT_SECURITY_KEY);
            //把user存在ctx的user属性中
            ctx.user = user
            console.log(user.id)
        } catch (error) {
            ctx.body = { status: 2, message: error.message };
            return;
        }
    }

    await next();
}

module.exports = getUserInfo;